using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace RUPS
{
    public partial class ModificaClientiForm : Form
    {
        public ModificaClientiForm()
        {
            InitializeComponent();
        }

        public int origID;
        public string origNume;
        public string origPrenume;
        public string origAdresa1;
        public string origAdresa2;
        public string origLocalitate;
        public string origTelefon;
        public string origEmail;
        public bool origPersFiz;
        public string A1isNull;
        public string A2isNUll;
        public int indexlinie;
                
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void ModificaClientiForm_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'masivDBDataSet11.clienti' table. You can move, or remove it, as needed.
            this.clientiTableAdapter.Fill(this.masivDBDataSet11.clienti);
            // TODO: This line of code loads data into the 'masivDBDataSetModificare.clienti' table. You can move, or remove it, as needed.
            //this.clientiTableAdapter.Fill(this.masivDBDataSetModificare.clienti);
            // TODO: This line of code loads data into the 'masivDBDataSet4.clienti' table. You can move, or remove it, as needed.
            //this.clientiTableAdapter4.Fill(this.masivDBDataSet4.clienti);
            // TODO: This line of code loads data into the 'masivDBDataSet3.clienti' table. You can move, or remove it, as needed.
            //this.clientiTableAdapter3.Fill(this.masivDBDataSet3.clienti);
            // TODO: This line of code loads data into the 'masivDBDataSet2.clienti' table. You can move, or remove it, as needed.
            //this.clientiTableAdapter.Fill(this.masivDBDataSet2.clienti);

        }

        private void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            
            DataGridViewRow r = dataGridView1.SelectedRows[0];
            origID = Convert.ToInt32(r.Cells[0].Value.ToString());
            //toolStripStatusLabel1.Text = r.Cells[8].Value.ToString();
            textBoxNumeClient.Text = r.Cells[1].Value.ToString();
            origNume = r.Cells[1].Value.ToString();
            textBoxPrenumeClient.Text = r.Cells[2].Value.ToString();
            origPrenume = r.Cells[2].Value.ToString();
            textBoxTelefonClient.Text = r.Cells[6].Value.ToString();
            origTelefon = r.Cells[6].Value.ToString();
            textBoxEmailClient.Text = r.Cells[7].Value.ToString();
            origEmail = r.Cells[7].Value.ToString();
            textBoxAdresa1Client.Text = r.Cells[3].Value.ToString();
            origAdresa1 = r.Cells[3].Value.ToString();
            textBoxAdresa2Client.Text = r.Cells[4].Value.ToString();
            origAdresa2 = r.Cells[4].Value.ToString();
            textBoxLocalitateClient.Text = origLocalitate = r.Cells[5].Value.ToString();
            if (textBoxAdresa1Client.Text == "")
                A1isNull = "1";
            else A1isNull = "0";
            if (textBoxAdresa2Client.Text == "")
                A2isNUll = "1";

            else A2isNUll = "0";
            indexlinie = e.RowIndex;
            if (r.Cells[8].Value.ToString() == "True")
                checkBoxPersoanaJuridica.Checked = origPersFiz = true;
            else
                checkBoxPersoanaJuridica.Checked = origPersFiz = false;
        }

        

        private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void textBoxTelefonClient_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar >= '0' && e.KeyChar <= '9' || e.KeyChar == 8 || e.KeyChar == '.')
            {
                base.OnKeyPress(e);
            }
            else
                e.Handled = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            bool eroare = false;
            
            toolStripStatusLabel1.ForeColor = Color.Red;
            System.Text.RegularExpressions.Regex expresie = new System.Text.RegularExpressions.Regex(@"\S+@\S+\.\S+");
            if ((textBoxNumeClient.Text == "") || (textBoxPrenumeClient.Text == "") || (textBoxTelefonClient.Text == "") || (textBoxEmailClient.Text == ""))
            {
                eroare = true;
                errorProvider1.SetError(groupBox1, "Acestea sunt date obligatorii!");

            }
            else errorProvider1.SetError(groupBox1, "");
            if (textBoxTelefonClient.Text.Length < 10 && !eroare)
            {
                eroare = true;
                errorProvider1.SetError(textBoxTelefonClient, "Numarul de telefon nu este complet! (sunt asteptate 10 cifre)");

            }
            else errorProvider1.SetError(textBoxTelefonClient, "");

            if (!expresie.IsMatch(textBoxEmailClient.Text) && !eroare)
            {
                eroare = true;
                errorProvider1.SetError(textBoxEmailClient, "Acesta nu este o adresa de mail valida!");

            }
            else errorProvider1.SetError(textBoxEmailClient, "");
            //else if (textBoxTelefonClient.Text.
            //else
            //    errorProvider1.SetError(groupBox1, "");

            if (!eroare)
            {
                
                CreareClientTableAdapters.clientiTableAdapter tac = new RUPS.CreareClientTableAdapters.clientiTableAdapter();
                try
                {
                    
                    tac.UpdateClienti(textBoxNumeClient.Text, textBoxPrenumeClient.Text, textBoxAdresa1Client.Text, textBoxAdresa2Client.Text, textBoxLocalitateClient.Text, textBoxTelefonClient.Text, textBoxEmailClient.Text, checkBoxPersoanaJuridica.Checked,
                        origID, origNume, origPrenume, A1isNull, A2isNUll, origLocalitate, origTelefon, origEmail, origPersFiz, origAdresa1, origAdresa2, origID);
                        
                    toolStripStatusLabel1.ForeColor = Color.Black;
                    toolStripStatusLabel1.Text = "Clientul a fost modificat!";
                    clientiBindingSource.ResetBindings(false);
                    dataGridView1.DataSource = null;
                    CreareClient.clientiDataTable dt=new CreareClient.clientiDataTable();
                    tac.FillClienti(dt);
                    clientiBindingSource.DataSource = dt;
                    //dataGridView1.InvalidateRow(dataGridView1.CurrentRow.Index);
                    dataGridView1.DataSource = clientiBindingSource;
                    dataGridView1.Invalidate();
                    //dataGridView1.re
                    //dataGridView1.b
                    //dataGridView1.
                }
                catch (Exception)
                {
                    
                    toolStripStatusLabel1.Text = "Eroare: Nu s-a reusit introducerea!";
                }

            }
            else
            {
                //label8.ForeColor = Color.Red;
                toolStripStatusLabel1.Text = "Eroare! Verificati semnul de exclamare.";

            } 
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Stergi clientul?", "Confirmare", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                try
                {
                    CreareClientTableAdapters.clientiTableAdapter tac = new RUPS.CreareClientTableAdapters.clientiTableAdapter();
                    tac.DeleteClient(origID, origNume, origPrenume, A1isNull, origAdresa1, A2isNUll, origAdresa2, origLocalitate, origTelefon, origEmail, origPersFiz);
                    toolStripStatusLabel1.Text = "Clientul a fost sters!";
                    clientiBindingSource.ResetBindings(false);
                    dataGridView1.DataSource = null;
                    CreareClient.clientiDataTable dt = new CreareClient.clientiDataTable();
                    tac.FillClienti(dt);
                    clientiBindingSource.DataSource = dt;
                    //dataGridView1.InvalidateRow(dataGridView1.CurrentRow.Index);
                    dataGridView1.DataSource = clientiBindingSource;
                    dataGridView1.Invalidate();
                }
                catch
                {
                    toolStripStatusLabel1.ForeColor = Color.Red;
                    toolStripStatusLabel1.Text = "Eroare: Stergerea nu s-a produs! Stergeti mai intai comenzile pe care le are clientul!";
                }
            }
        }

        private void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellEventArgs e)
        {
            

        }
    }
}